package ldk.easytouch.example.activity;


import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Arrays;

import ldk.easytouch.activity.AbstractMenuActivity;
import ldk.easytouch.example.R;
import ldk.easytouch.example.interfaces.IMenuItem;
import ldk.easytouch.example.menus.EmptyMenuItem;
import ldk.easytouch.example.menus.MikepenzMenuItem;
import ldk.easytouch.utils.ETLog;

/**
 * Created by ke on 16-9-15.
 */
public class MenuActivity extends AbstractMenuActivity{
    @SuppressWarnings("unused")
    private static final String TAG = "MenuActivity";

    private FrameLayout[] mMenuItemsLayout = new FrameLayout[9];
    private static final int[] s_Menu_Item_Id = new int[]{
            R.id.fl_row1_1, R.id.fl_row1_2, R.id.fl_row1_3,
            R.id.fl_row2_1, R.id.fl_row2_2, R.id.fl_row2_3,
            R.id.fl_row3_1, R.id.fl_row3_2, R.id.fl_row3_3};

    private IMenuItem[] mMenuItems = new IMenuItem[9];
    private MenuItemViewHolder[] mMenuItemViewHolders = new MenuItemViewHolder[9];
    private boolean mIsEditMode = false;

    private MikepenzMenuItem s_BackMenuItem, s_FingerMenuItem;
    @Override
    public void onCreate() {
        super.onCreate();
        setContentView(R.layout.activity_menu);
        for(int i = 0; i < mMenuItemsLayout.length; i++){
            mMenuItemsLayout[i] = findView(s_Menu_Item_Id[i]);
        }

        initMenuItemData();
        refreshMenuItem();
    }

    private void initMenuItemData() {
        Arrays.fill(mMenuItems, new EmptyMenuItem(getContext()));
        s_BackMenuItem = new MikepenzMenuItem(getContext(), getContext().getString(R.string.gmd_reply));
        s_BackMenuItem.setCanDelete(false);
        s_BackMenuItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIsEditMode = false;
                mMenuItems[4] = s_FingerMenuItem;
                refreshMenuItem();
            }
        });
        s_FingerMenuItem = new MikepenzMenuItem(getContext(), getContext().getString(R.string.gmd_fingerprint));
        s_FingerMenuItem.setCanDelete(false);
        s_FingerMenuItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        s_FingerMenuItem.setLongClickListener(new View.OnLongClickListener(){

            @Override
            public boolean onLongClick(View v) {
                ETLog.v(TAG, "finger onLong Click");
                mIsEditMode = true;
                mMenuItems[4] = s_BackMenuItem;
                refreshMenuItem();
                return true;
            }
        });
        mMenuItems[4] = s_FingerMenuItem;

        mMenuItems[0] = ActivityTotalConstant.getActivityMenuItem(getContext()).get(0);
    }

    private void refreshMenuItem() {
        for(int i = 0; i < mMenuItemsLayout.length; i++){
            MenuItemViewHolder viewHolder = mMenuItemViewHolders[i];
            IMenuItem menuItem = mMenuItems[i];
            if(viewHolder == null){
                viewHolder = new MenuItemViewHolder();
                View view = LayoutInflater.from(getContext()).inflate(R.layout.list_item_menu, mMenuItemsLayout[i]);
                viewHolder.iconImageView = (ImageView) view.findViewById(R.id.iv_icon);
                viewHolder.nameTextView = (TextView) view.findViewById(R.id.tv_name);
                viewHolder.deleteImageView = (ImageView) view.findViewById(R.id.iv_close);
                viewHolder.itemView = view;
                mMenuItemViewHolders[i] = viewHolder;
                final int finalI = i;
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMenuItems[finalI].onSingleTapConfirmed();
                    }
                });
                view.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if(mMenuItems[finalI].onLongPressed()) return true;
                        mIsEditMode = true;
                        mMenuItems[4] = s_BackMenuItem;
                        refreshMenuItem();
                        return true;
                    }
                });
            }
            viewHolder.itemView.setVisibility((!mIsEditMode && menuItem instanceof EmptyMenuItem) ? View.INVISIBLE : View.VISIBLE);
            viewHolder.iconImageView.setImageDrawable(menuItem.icon());
            viewHolder.deleteImageView.setVisibility(mIsEditMode && menuItem.canDelete() ? View.VISIBLE : View.INVISIBLE);
            viewHolder.nameTextView.setText(menuItem.name());
        }
    }

    private class MenuItemViewHolder{
        ImageView iconImageView, deleteImageView;
        TextView nameTextView;
        View itemView;
    }

}
